package com.bawei.libs_base.mvvm

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

abstract class BaseActivity<VDB: ViewDataBinding,VM:BaseViewModel>: AppCompatActivity() {
    private val type by lazy {
        (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
    }

    val binding by lazy {
        (type[0] as Class<VDB>).getMethod("inflate",LayoutInflater::class.java).invoke(null,layoutInflater) as VDB
    }
    val viewModel by lazy {
        ViewModelProvider(this)[type[1] as Class<VM>]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        initData()
    }

    abstract fun initView()

    abstract fun initData()

}